﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Xfs;

namespace XfsClient
{
    [XfsObjectSystem]
    public class XfsTestAwakeSystem : XfsAwakeSystem<XfsTest>
    {
        public override void Awake(XfsTest self)
        {


        }
    }

    [XfsObjectSystem]
    public class XfsTestStartSystem : XfsStartSystem<XfsTest>
    {
        public override async void Start(XfsTest self)
        {
            //await XfsTimerComponent.Instance.WaitAsync(4000);

            //Moveing(self).Coroutine();

            //TestTime(self).Coroutine();

        }

        async XfsVoid Moveing(XfsTest self)
        {
            while (true)
            {
                await XfsTimerComponent.Instance.WaitAsync(1000);

                int with = (int)Math.Floor((double)(10 / 3));

                Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + self.GetType().Name + " 47. 在测试 XfsTimerComponent 的用法... "+ with);
            }
        }

        async XfsVoid TestTime(XfsTest self)
        {
            Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + self.GetType().Name + " 38. 在测试 XfsTimerComponent 的用法...开始 记时");

            await XfsTimerComponent.Instance.WaitAsync(4000);

            Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + self.GetType().Name + " 38. 在测试 XfsTimerComponent 的用法...记时结束");
        }

    }

    [XfsObjectSystem]
    public class XfsTestUpdateSystem : XfsUpdateSystem<XfsTest>
    {
        public override async void Update(XfsTest self)
        {

            //TestUserCall(self).Coroutine();
            //TestTime(self).Coroutine();


        }
         

        async XfsVoid TestTime(XfsTest self)
        {
            if (!isTime)
            {
                await XfsTimerComponent.Instance.WaitAsync(4000);

                Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + self.GetType().Name + " 38. 在测试 XfsTimerComponent 的用法...开始 记时");

                await XfsTimerComponent.Instance.WaitAsync(10000);

                Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + self.GetType().Name + " 38. 在测试 XfsTimerComponent 的用法...记时结束");

                isTime = true;
            }

        }

        bool isTime = false;

        int count = 0;
        int time = 0;
        int restime = 4000;
        async void TsstTimerComponent()
        {
            //await XfsTestTimerComponent.Instance.WaitAsync(4000);
            await XfsTimerComponent.Instance.WaitAsync(4000);

            Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + this.GetType().Name + " 45. 开始记时4秒。" + count++);

            //this.time += 1;

            //if (this.time > this.restime)
            //{
            //    Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + this.GetType().Name + " 42. 开始记时4秒。" + count++);

            //    await XfsTestTimerComponent.Instance.WaitAsync(1000);

            //    Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + this.GetType().Name + " 45. 开始记时4秒。" + count++);

            //    this.time = 0;
            //}


        }

        async void TestInnerSessionSend(XfsTest self)
        {
            time += 1;
            if (time > restime)
            {
                time = 0;

                XfsSession session = XfsGame.XfsSence.GetComponent<XfsNetInnerComponent>().Get();

                C4S_Ping resqustC = new C4S_Ping();
                resqustC.Opcode = XfsGame.XfsSence.GetComponent<XfsOpcodeTypeComponent>().GetOpcode(resqustC.GetType());
                resqustC.Message = XfsTimeHelper.Now().ToString();

                Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + this.GetType().Name + " 43. 开始打电话给内网." + resqustC.Message);
                Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + this.GetType().Name + " 44. 呼出的消息体长度." + resqustC.ToString().Length);

                S4C_Ping? responseC = await session.CallInner(resqustC) as S4C_Ping;

                Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + this.GetType().Name + " 48. 收到内网回来的信息." + responseC.Message);
                Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + this.GetType().Name + " 49. 呼入的消息体长度." + responseC.ToString().Length);

            }
        }
        async void TestOuterSessionSend(XfsTest self)
        {
            time += 1;
            if (time > restime)
            {
                time = 0;

                Dictionary<long, XfsSession> sessions = XfsGame.XfsSence.GetComponent<XfsNetOuterComponent>().Sessions;
                if (sessions.Count > 0)
                {
                    XfsSession session = sessions.Values.ToList()[0];

                    C4S_Ping resqustC = new C4S_Ping();
                    resqustC.Opcode = XfsGame.XfsSence.GetComponent<XfsOpcodeTypeComponent>().GetOpcode(resqustC.GetType());
                    resqustC.Message = self.call;

                    if (session.RemoteAddress != null)
                    {
                        resqustC.Message = self.call + " " + session.RemoteAddress.ToString();
                    }

                    Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + this.GetType().Name + " 67. 开始打电话给外网." + resqustC.Message);
                    Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + this.GetType().Name + " 68. 呼出的消息体长度." + resqustC.ToString().Length);

                    S4C_Ping? responseC = await session.Call(resqustC) as S4C_Ping;

                    if (responseC != null)
                    {
                        ///从服务端发回来的信息
                        Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + this.GetType().Name + " 73. 收到外网回来的信息." + responseC.Message);
                        Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + this.GetType().Name + " 74. 呼入的消息体长度." + responseC.ToString().Length);

                    }

                }

            }
        }
        async void TestActorOuterSessionSend(XfsTest self)
        {
            this.time += 1;
            if (this.time > this.restime)
            {
                this.time = 0;

                Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + this.GetType().Name + " 106. 开始记时4秒。");

                await XfsGame.XfsSence.GetComponent<XfsTimerComponent>().WaitAsync(4000);

                Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + this.GetType().Name + " 108. 时间到。");

                Dictionary<long, XfsSession> sessions = XfsGame.XfsSence.GetComponent<XfsNetOuterComponent>().Sessions;
                if (sessions.Count > 0 && self.severTestoneId != 0)
                {
                    XfsSession session = sessions.Values.ToList()[0];

                    C4S_PingActor resqustC = new C4S_PingActor();
                    resqustC.Opcode = XfsGame.XfsSence.GetComponent<XfsOpcodeTypeComponent>().GetOpcode(resqustC.GetType());


                    resqustC.ActorId = self.severTestoneId;
                    resqustC.Message = self.call;

                    if (session.RemoteAddress != null)
                    {
                        resqustC.Message = self.call + " " + session.RemoteAddress.ToString();
                    }

                    Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + this.GetType().Name + " 126. 开始打电话给外网.ActorId: " + resqustC.ActorId);
                    Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + this.GetType().Name + " 127. 呼出的消息体长度." + resqustC.ToString().Length);

                    S4C_PingActor? responseC = await session.Call(resqustC) as S4C_PingActor;

                    if (responseC != null)
                    {
                        ///从服务端发回来的信息
                        Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + this.GetType().Name + " 135. 收到外网回来的信息." + responseC.Message);
                        Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + this.GetType().Name + " 136. 呼入的消息体长度." + responseC.ToString().Length);

                    }

                }

          }
        }

        bool isLogin = false;
        private async XfsVoid TestUserCall(XfsTest self)
        {
            time += 1;
            if (time > restime && !isLogin)
            {
                time = 0;
                Dictionary<long, XfsSession> sessions = XfsGame.XfsSence.GetComponent<XfsNetOuterComponent>().Sessions;

                if (sessions.Count > 0)
                {
                    XfsSession session = sessions.Values.ToList()[0];

                    C4S_User resqustC = new C4S_User() { Username = "Tumo1", Password = "1234" };

                    if (!session.IsRunning) return;

                    Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + self.GetType().Name + " 20. User." + resqustC.Username + " 开始登录...");

                    S4C_User responseC = await session.Call(resqustC) as S4C_User;

                    if (responseC != null)
                    {
                        ///从服务端发回来的信息

                        Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + self.GetType().Name + " 26. User." + responseC.Username + " 登录成功...");

                        XfsUser user = XfsComponentFactory.Create<XfsUser>();

                        user.Username = responseC.Username;
                        user.Password = responseC.Password;
                        user.Id = responseC.Userid;

                        XfsGame.XfsSence.GetComponent<XfsUserComponent>().Users.Add(responseC.Username, user);


                        isLogin = true;
                    }

                }

            }
        }

        //private async XfsVoid TestUser02Call(XfsTest self)
        //{
        //    time += 1;
        //    if (time > restime && !isLogin)
        //    {
        //        time = 0;
        //        Dictionary<long, XfsSession> sessions = XfsGame.XfsSence.GetComponent<XfsNetOuterComponent>().Sessions;

        //        if (sessions.Count > 0)
        //        {
        //            XfsSession session = sessions.Values.ToList()[0];
                    
        //            session.UserLogin("Tumo1", "1234").Coroutine();

        //            //S4C_User responseC = session.UserLogin<S4C_User>("Tumo1", "1234");

        //            //C4S_User resqustC = new C4S_User() { Username = "Tumo1", Password = "1234" };

        //            //if (!session.IsRunning) return;

        //            //Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + self.GetType().Name + " 20. User." + resqustC.Username + " 开始登录...");

        //            //S4C_User responseC = await session.Call(resqustC) as S4C_User;

        //            if (responseC != null)
        //            {
        //                ///从服务端发回来的信息

        //                Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + self.GetType().Name + " 26. User." + responseC.Username + " 登录成功...");

        //                XfsUser user = XfsComponentFactory.Create<XfsUser>();

        //                user.Username = responseC.Username;
        //                user.Password = responseC.Password;
        //                user.Id = responseC.Userid;

        //                XfsGame.XfsSence.GetComponent<XfsUserComponent>().Users.Add(responseC.Username, user);


        //                isLogin = true;
        //            }

        //        }

        //    }
        //}



    }

}
